﻿using AutoMapper;
using DearlerPlatform.Domain;
using DearlerPlatfrom.Core;
using DearlerPlatfrom.Core.Core;
using DearlerPlatfrom.Core.Repository;
using DearlerPlatfrom.Service.Common;
using DearlerPlatfrom.Service.ProductApp.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DearlerPlatfrom.Service.ProductApp
{
    public partial class ProductService : IProductService
    {
        private readonly IRepository<Product> _productRepo;//产品
        private readonly IRepository<ProductSale> _productSaleRepo;//产品销售
        private readonly IRepository<ProductPhoto> _productPhotoRepo;//产品图片
        private readonly IRepository<ProductSaleAreaDiff> _productSaleAreaDiffRepo;//产品销售区域差异
        private readonly IMapper _mapper;
        private readonly DearlerPlarfromDbContext _context;

        public ProductService(
              IRepository<Product> ProductRepo,
              IRepository<ProductSale> ProductSaleRepo,
              IRepository<ProductPhoto> ProductPhotoRepo,
              IRepository<ProductSaleAreaDiff> ProductSaleAreaDiffRepo,
              IMapper mapper,
              DearlerPlarfromDbContext context
            )
        {
            _productRepo = ProductRepo;
            _productSaleRepo = ProductSaleRepo;
            _productPhotoRepo = ProductPhotoRepo;
            _productSaleAreaDiffRepo = ProductSaleAreaDiffRepo;
            _mapper = mapper;
            _context = context;
        }

        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="pagingDto"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ProductDto>> GetProductDto(string blongType, PageWithSortDto dto)
        {
            dto.Sort ??= "ProductName";
            var products = await _productRepo.Where(m => m.BelongTypeNo == blongType).ToListAsync();
            //products = products
            var dtos = _mapper.Map<List<ProductDto>>(products);
            var productNos = dtos.Select(m => m.ProductNo).ToArray();
            var productPhotos = await GetProductPhotoByProductNo(productNos);
            var productSales = await GetProductSaleByProductNo(productNos);
            dtos.ForEach(m =>
            {
                m.productPhotos = productPhotos.FirstOrDefault(p => p.ProductNo == m.ProductNo);
                m.ProductSales = productSales.FirstOrDefault(p => p.ProductNo == m.ProductNo);
            });
            return dtos;
        }

        public async Task<IEnumerable<ProductTypeDto>> GetProductType()
        {
            var productTypeDtos = _context.Products.Select(m => new ProductTypeDto
            {
                TypeName = m.TypeName,
                TypeNo = m.TypeNo
            }).Distinct().ToList();
            return productTypeDtos;
        }

        public async Task<Dictionary<string, IEnumerable<string>>> ListProductTypes(string belongTypeName, string? typeNo)
        {
            var dicProductType = new Dictionary<string, IEnumerable<string>>();
            var products = await _productRepo.GetListAsync(m => m.BelongTypeName == belongTypeName && (m.TypeNo == typeNo || string.IsNullOrWhiteSpace(typeNo)));
            dicProductType.Add("ProductBzgg|包装规格", products.Select(m => m.ProductBzgg).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductCd|产地", products.Select(m => m.ProductCd).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductCz|材质", products.Select(m => m.ProductCz).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductDj|等级", products.Select(m => m.ProductDj).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductGg|规格", products.Select(m => m.ProductGg).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductGy|工艺", products.Select(m => m.ProductGy).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductHb|环保", products.Select(m => m.ProductHb).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductHd|厚度", products.Select(m => m.ProductHd).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductHs|花色", products.Select(m => m.ProductHs).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductMc|面材", products.Select(m => m.ProductMc).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductPp|品牌", products.Select(m => m.ProductPp).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductXh|型号", products.Select(m => m.ProductXh).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            dicProductType.Add("ProductYs|颜色", products.Select(m => m.ProductYs).Distinct().Where(m => !string.IsNullOrWhiteSpace(m)).ToList());
            return dicProductType;
        }
    }
}
